package com.relin.websocket;
import java.io.IOException;
import java.net.InetSocketAddress;  
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Executors;  
  





import org.jboss.netty.bootstrap.ServerBootstrap;  
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;  





import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import com.relin.websocket.util.JedisUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;


  
public class WebSocketServer  
{  
    private final int port;  
  
    public WebSocketServer(int port) {  
        this.port = port;  
    }  
  
    public void run() {  
        // 设置 Socket channel factory  
        ServerBootstrap bootstrap = new ServerBootstrap(  
                new NioServerSocketChannelFactory(  
                        Executors.newCachedThreadPool(),  
                        Executors.newCachedThreadPool()));  
  
        // 设置 Socket pipeline factory  
        bootstrap.setPipelineFactory(new WebSocketServerPipelineFactory());  
  
        // 启动服务，开始监听  
        bootstrap.bind(new InetSocketAddress(port));  
        Subscribe();
        // 打印提示信息  
        System.out.println("Web socket server started at port " + port + '.');  
        System.out.println("Open your browser and navigate to http://localhost:" + port + '/');  
    }  
  
    public static void main(String[] args) {  
        int port;  
        if (args.length > 0) {  
            port = Integer.parseInt(args[0]);  
        } else {  
            port = 8080;  
        }  
        new WebSocketServer(port).run();  
    }  
    public void Subscribe() {
        new Thread(new Runnable() {
        	 @Override
            public void run() {
                Jedis jedis = JedisUtils.getJedis();

                jedis.subscribe(new JedisPubSub() {

                    @Override
                    public void onUnsubscribe(String channel, int subscribedChannels) {
                        System.out.println("onUnsubscribe:" + channel);
                    }

                    @Override
                    public void onSubscribe(String channel, int subscribedChannels) {
                        System.out.println("onSubscribe:" + channel);
                    }

                    @Override
                    public void onPUnsubscribe(String pattern, int subscribedChannels) {
                        System.out.println("onPUnsubscribe:" + pattern);
                    }

                    @Override
                    public void onPSubscribe(String pattern, int subscribedChannels) {
                        System.out.println("onPSubscribe:" + pattern);
                    }

                    @Override
                    public void onPMessage(String pattern, String channel, String message) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onMessage(String channel, String message) {
                        System.out.print("onMessage:" + channel);
                        System.out.println("\t" + message);
                        try {
                        	 Map<ChannelHandlerContext, MessageEvent> messageMap = WebSocketServerHandler.map;
                        	 for (Entry<ChannelHandlerContext, MessageEvent> en : messageMap.entrySet()) {
	                            MessageEvent e = en.getValue();
	                            ChannelHandlerContext channelHandlerContext = en.getKey();
	                            channelHandlerContext.getChannel().write(new TextWebSocketFrame(message.toUpperCase()));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, WebSocketServerHandler.KEY);
            }
        }).start();;
    }
}  
